package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.business.interfaces.worktrack.WorkTrackBService;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.entity.omstrack.OrderWorkTrack;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.datasync.ElasticSearchDataSyncService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.collectionnotpay.CollectionNotPayBService;
import com.zmn.track.common.constant.TrackConfigConsts;
import com.zmn.track.common.constant.TrackConsts;
import com.zmn.track.common.dto.trackwork.QueueTrackDataDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName OmsOrderWorkTrackListener
 * @description: 跟单系统冗余到oms的跟单数据
 * @author: wangyanci
 * @create: 2021-05-13 14:27
 * @Version 1.0
 **/
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.track}")
@Component
public class OmsOrderWorkTrackListener {

    // 限定内容ids
    private final static List<Integer> RESTRICT_TRACK_CONTENT_IDS = Lists.newArrayList(
            TrackConsts.TRACK_CONTENT_ID_USER_CANCEL,
            TrackConsts.TRACK_CONTENT_ID_ENGINEER_CANCEL,
            TrackConsts.TRACK_CONTENT_ID_DELIVERY,
            TrackConsts.TRACK_CONTENT_ID_WAIT_PART,
            TrackConsts.TRACK_CONTENT_ID_STATEMENT,
            TrackConsts.TRACK_CONTENT_ID_INVOICE,
            TrackConsts.TRACK_CONTENT_ID_SERVICE_COMPLETE,
            TrackConsts.TRACK_CONTENT_ID_REWORK,
            TrackConsts.TRACK_CONTENT_ID_COMPANY_MONEY,
            TrackConsts.TRACK_CONTENT_ID_PLAT_MONEY,
            TrackConsts.TRACK_CONTENT_ID_USER_APPLY_CANCEL
    );

    @Autowired
    private WorkTrackBService workTrackBService;
    @Autowired
    private ElasticSearchDataSyncService elasticSearchDataSyncService;
    @Autowired
    private CollectionNotPayBService collectionNotPayBService;
    @Autowired
    private OrderWorkService orderWorkService;
    @Resource
    private RedisManager redisManager;

    /**
     * 同步跟单系统数据变更
     *
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.TRACK_NODE_NOTICE_MQ_TAG)
    public void omsTrack(String message){
        log.info("OmsOrderWorkTrackListener.omsTrack message={}", message);

        try {
            QueueTrackDataDTO messageDto = JSON.parseObject(message, QueueTrackDataDTO.class);
            OrderWorkTrack track = BeanMapper.map(messageDto, OrderWorkTrack.class);
            track.setTrackType(messageDto.getTrackContentId());

            String redisKey = String.format(RedisKeyConsts.ORDER_TRACK_MQ_MESSAGE_KEY, messageDto.getTrackWorkId(),messageDto.getTrackStatus());
            if (redisManager.exists(redisKey)) {
                return;
            }
            redisManager.setex(redisKey,String.valueOf(track.getOrderId()), 3600);

            if (Objects.equals(messageDto.getTrackContentId(), TrackConfigConsts.TRACK_CONTENT_ID_ENGINEER_MONEY_NO_PAY)) {
                OrderWork orderWork = orderWorkService.findOrderWorkByKey(track.getOrderId(), track.getWorkId());
                if (Objects.nonNull(orderWork)) {
                    collectionNotPayBService.sendAppPushMessage(orderWork,messageDto.getAmount(),messageDto.getTrackWorkId());
                    collectionNotPayBService.robotCall(orderWork,messageDto.getAmount(),messageDto.getTrackWorkId());
                }
            }

            if (RESTRICT_TRACK_CONTENT_IDS.contains(track.getTrackType())) {
                // 跟单处理
                workTrackBService.trackChangeHandle(track);

                // mq同步es更改跟单数据
                elasticSearchDataSyncService.syncWorkIndex(track.getOrderId());
            }


        } catch (Exception e) {
            log.info("omsTrack occur exception:{}", e.getMessage());
        }
    }
}